using System;
using ValidationFramework.Extensions;
using ValidationFramework.Configuration.Xml;
using ValidationFramework.Configuration;

namespace ValidationFramework.Specifications
{
    /// <summary>
    /// A <see cref="IRuleConfigReader"/> that creates a <see cref="RequiredStringRule"/> from a <see cref="RuleData"/>.
    /// </summary>
    /// <remarks>
    ///   <see cref="RuleData"/> for default attributes.
    ///   <b>Extra Attributes</b>
    ///   <ul>
    ///     <li>
    ///       <c>initialValue</c> (optional): Used to populate <see cref="RequiredRule{T}.InitialValue"/>.   
    ///     </li>
    ///   </ul>
    /// </remarks>
    /// <example>
    /// <code lang="xml" title="This example shows an xml configuration for RequiredStringRuleConfigReader">
    /// <rule 
    /// errorMessage="hello" 
    /// typeName="RequiredStringRule" 
    /// initialValue="hello2"
    /// trimWhiteSpace="false"/>
    /// </code>
    /// </example>
    /// <seealso cref="RequiredStringRule"/>
    /// <seealso cref="ConfigurationService"/>
    public sealed class IsRequiredStringParser : ISpecificationXmlParser
    {
		public ISpecification GetSpecification(System.Xml.XmlElement SpecificationElement, RuleContext context)
		{
			Guard.ArgumentNotNull(SpecificationElement, "SpecificationElement");
			var initialValue = SpecificationElement.GetAttribute<string>("initialValue", "");
			var trimWhiteSpace = SpecificationElement.GetAttribute("trimWhiteSpace", true);
			var ignoreCase = SpecificationElement.GetAttribute<bool?>("trimWhiteSpace", null);

			if (initialValue == null && ignoreCase != null)
				throw new InvalidOperationException("ignoreCase cannot be set if there is no initialValue.");


			return new IsRequiredStringSpec(initialValue, trimWhiteSpace, ignoreCase);
		}
	}
}